<template>
  <el-dialog :title="isEdit ? '修改联系人': '新增联系人'" :visible.sync="visible" :close-on-click-modal="closeOnClickModal" @open="_handleDialogOpen" @close="_cancel">
    <el-form ref="dataForm" :model="form" :status-icon="true" :rules="formRules" label-width="100px" >
      <el-form-item label="联系人" prop="name">
        <el-input v-model="form.name" placeholder="联系人姓名"/>
      </el-form-item>
      <el-form-item label="联系手机号" prop="phoneNo">
        <el-input v-model="form.phoneNo" placeholder="联系人手机号"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="_cancel">取 消</el-button>
      <el-button :loading="_loading" size="small" type="primary" @click="_ok">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

// import idKit from '@/utils/id-kit'

export default {

  props: {
    closeOnClickModal: {
      type: Boolean,
      default: false
    },

    loading: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      isEdit: false,
      visible: false,
      form: {},
      formRules: {
        name: [{
          required: true,
          message: '联系人名称不能为空'
        }],
        phoneNo: [{
          required: true,
          message: '联系人手机号不能为空'
        }, {
          validator: (rule, value, callback) => {
            if (value && (!/[0-9]+/.test(value) || value.trim().length !== 11)) {
              callback(new Error('手机号格式不正确'))
            } else {
              callback()
            }
          },
          trigger: 'change'
        }]
      }
    }
  },

  computed: {
    '_loading': {
      get() {
        return this.loading
      },

      set(value) {
        this.$emit('update:loading', value)
      }
    }
  },

  methods: {
    _formReset() {
      this.form = {
        name: '',
        phoneNo: '',
        sortNo: 1
      }
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
      })
    },

    _handleDialogOpen() {
      this._formReset()
      this.$emit('open')
    },

    _cancel() {
      this._formReset()
      this.visible = false
      this.isLoading = false
      this.$emit('cancel')
    },

    _ok() {
      const form = this.$refs.dataForm
      form.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(this.form))
          this.$emit('ok', formData, this.isEdit)
        }
      })
    },

    show(params) {
      this.visible = true
      // this.parentData = parent
      if (params) {
        this.isEdit = true
        setTimeout(() => {
          this.form = params || {}
        }, 0)
        // organApi.getById({
        //   id: params.id
        // }).then(resp => {
        //   this.form = resp.data
        // }).catch(resp => {
        //   this.$message.error(resp.message)
        // })
      } else {
        // pass
        this.isEdit = false
        // this._clear()
      }
    },

    close() {
      this._cancel()
    }
  }
}
</script>

<style lang="scss" scoped>
.form-item {
  position: relative;
  border: 1px solid #dddddd;
  padding: 25px 15px 10px 5px;
  margin: 5px 0;
  border-radius: 5px;

  .form-item-close {
    position: absolute;
    top: 0;
    right: 5px;
    padding: 2px;
    cursor: pointer;
    &:hover {
      color: red;
    }
  }
}
</style>
